
package dao.impl;

import dao.IBookDao;
import domain.Book;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/*
 * 书本数据访问层的具体实现方式
 * 实现了数据访问层的接口
 */



/*
 * 书本数据访问层的具体实现方式
 * 实现了数据访问层的接口
 */
public class BookDao implements IBookDao {
    @Override
    public List selectBooks() {
        //需要到数据库中获取数据
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "select * from book";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);
        //获取结果集
        ResultSet rs = JDBCUtil.getRs(pstmt);

        //初始化一个list容器用于放置Teacher的数据
        List<Book> bookList = new ArrayList<Book>();
        //使用rs.next（）迭代数据，如果有下一条，则返回真，否则返回假
        try {
            while (rs.next()) {
                //将所有的数据库数据转化为java中的对象，完成数据结构的转换
                Book book = new Book();
                book.setName(rs.getString("name"));
                book.setBookno(rs.getString("bookno"));
                book.setPrivce(rs.getString("privce"));
                book.setAuth(rs.getString("auth"));
                book.setState(rs.getString("state"));
                book.setCbs(rs.getString("cbs"));
                book.setContent(rs.getString("content"));
                //将数据赋给list容器
                bookList.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pstmt, connection);
        }
        return bookList;
    }

    @Override
    public int insertBook(Book book) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "insert into book (name,bookno,privce,auth,cbs,state,content) values(?,?,?,?,?,?,?) ";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集
        try {

            pstmt.setString(1, book.getName());
            pstmt.setString(2, book.getBookno());
            pstmt.setString(3, book.getPrivce());
            pstmt.setString(4, book.getAuth());
            pstmt.setString(5, book.getCbs());
            pstmt.setString(6, book.getState());
            pstmt.setString(7, book.getContent());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        int result = JDBCUtil.modify(pstmt);
        return result;
    }

    //删除图书
    public String deleteBook(String num) {//初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "delete from book where bookno='"+num+"'";

        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        int result = JDBCUtil.modify(pstmt);

        return sql;
    }
    //修改图书
    public int updateBook(Book book) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "update  book set name = '"+ book.getName()+"',privce='"+book.getPrivce()+"',auth ='"+book.getAuth()+"',state='"+book.getState()+"',cbs='"+book.getCbs()+"',content='"+book.getContent()+"'where bookno='"+book.getBookno()+"'";
        //获取数据库操作句柄
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集

        int result = JDBCUtil.modify(pstmt);
        return result;
    }
    //借阅图书
    public int updateBookState(Book book) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "update  book set state = '"+book.getState()+"'where bookno='"+book.getBookno()+"'";
        //获取数据库操作句柄
        System.out.println(sql);
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集
        try {
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        int result = JDBCUtil.modify(pstmt);
        return result;
    }
    //归还图书
    public int returnBookState(Book book) {
        //初始化连接
        Connection connection = JDBCUtil.getConnection();
        //告诉mysql我要干什么
        String sql = "update  book set state = '"+book.getState()+"'where bookno='"+book.getBookno()+"'";
        //获取数据库操作句柄
        System.out.println(sql);
        PreparedStatement pstmt = JDBCUtil.getPstmt(connection, sql);

        //获取结果集
        try {
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        int result = JDBCUtil.modify(pstmt);
        return result;
    }

}



